package com.wfy.controller;

import com.wfy.pojo.Brand;
import com.wfy.pojo.PageResult;
import com.wfy.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: BrandController
 * @Package com.wfy.controller
 * @Author 王飞遥
 * @Date 2022/8/17 20:31
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    BrandService brandService;
    /**
     * key 查询条件
     * page 当前页
     * rows 条数
     * sortBy 排序字段名称
     * desc 是否排序
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(@RequestParam(value = "key",required = false)String key,
                                                              @RequestParam(value = "page",defaultValue = "1")Integer page,
                                                              @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                              @RequestParam(value = "sortBy",required = false)String sortBy,
                                                              @RequestParam(value = "desc",required = false)Boolean desc){

         PageResult<Brand> result=brandService.queryBrandByPage(key,page,rows,sortBy,desc);
         if(CollectionUtils.isEmpty(result.getItems())){
             //CollectionUtils.isEmpty()判断是否为空
             return ResponseEntity.notFound().build();
         }
         return ResponseEntity.ok(result);
    }
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //根据商品分类的id查询绑定的品牌列表;
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid")Long cid){
        List<Brand> brandList=  brandService.queryBrandByCid(cid);
        if(CollectionUtils.isEmpty(brandList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brandList);
    }
    @GetMapping("/getBrandInfo/{brandId}")
    public ResponseEntity<Brand> getBrandInfo(@PathVariable("brandId")Long brandId){
       Brand brand= brandService.queryBrandInfoById(brandId);
       return ResponseEntity.ok(brand);

    }
}
